package org.pt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.pt.model.Comment;
import org.pt.model.Like;
import org.pt.model.Post;
import org.pt.mapper.CommentMapper;
import org.pt.mapper.LikeMapper;
import org.pt.mapper.PostMapper;
import org.pt.mapper.UserMapper;
import org.pt.service.SocialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class SocialServiceImpl implements SocialService {

    @Autowired
    private PostMapper postMapper;
    
    @Autowired
    private CommentMapper commentMapper;
    
    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public Post createPost(Post post) {
        post.setCreateTime(LocalDateTime.now());
        post.setUpdateTime(LocalDateTime.now());
        post.setLikeCount(0);
        post.setCommentCount(0);
        postMapper.insert(post);
        return post;
    }

    @Override
    public IPage<Post> getPosts(Long userId, int page, int size) {
        Page<Post> pageParam = new Page<>(page, size);
        QueryWrapper<Post> wrapper = new QueryWrapper<>();
        if (userId != null) {
            wrapper.eq("user_id", userId);
        }
        wrapper.orderByDesc("create_time");
        return postMapper.selectPage(pageParam, wrapper);
    }

    @Override
    public Post getPostById(Long postId) {
        return postMapper.selectById(postId);
    }

    @Override
    @Transactional
    public Comment createComment(Comment comment) {
        // 验证帖子是否存在
        Post post = postMapper.selectById(comment.getPostId());
        if (post == null) {
            throw new RuntimeException("帖子不存在");
        }
        
        // 验证用户是否存在
        if (userMapper.selectById(comment.getUserId()) == null) {
            throw new RuntimeException("用户不存在");
        }
        
        comment.setCreateTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());
        commentMapper.insert(comment);
        
        // 更新帖子评论数
        post.setCommentCount(post.getCommentCount() + 1);
        postMapper.updateById(post);
        
        return comment;
    }

    @Override
    public IPage<Comment> getCommentsByPostId(Long postId, int page, int size) {
        Page<Comment> pageParam = new Page<>(page, size);
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postId);
        wrapper.orderByDesc("create_time");
        return commentMapper.selectPage(pageParam, wrapper);
    }

    @Override
    @Transactional
    public void likePost(Long userId, Long postId) {
        // 验证帖子是否存在
        Post post = postMapper.selectById(postId);
        if (post == null) {
            throw new RuntimeException("帖子不存在");
        }
        
        // 验证用户是否存在
        if (userMapper.selectById(userId) == null) {
            throw new RuntimeException("用户不存在");
        }
        
        // 检查是否已经点赞
        if (hasLiked(userId, postId)) {
            return;
        }
        
        // 创建点赞记录
        Like like = new Like();
        like.setUserId(userId);
        like.setPostId(postId);
        like.setCreateTime(LocalDateTime.now());
        likeMapper.insert(like);
        
        // 更新帖子点赞数
        post.setLikeCount(post.getLikeCount() + 1);
        postMapper.updateById(post);
    }

    @Override
    @Transactional
    public void unlikePost(Long userId, Long postId) {
        // 删除点赞记录
        QueryWrapper<Like> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("post_id", postId);
        if (likeMapper.delete(wrapper) > 0) {
            // 更新帖子点赞数
            Post post = postMapper.selectById(postId);
            post.setLikeCount(post.getLikeCount() - 1);
            postMapper.updateById(post);
        }
    }

    @Override
    public boolean hasLiked(Long userId, Long postId) {
        QueryWrapper<Like> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("post_id", postId);
        return likeMapper.selectCount(wrapper) > 0;
    }
    
    @Override
    public Integer getLikeCount(Long postId) {
        // 方法一：从帖子表中直接获取
        Post post = postMapper.selectById(postId);
        if (post != null) {
            return post.getLikeCount();
        }
        
        // 方法二：如果需要确保准确性，可以从点赞表中统计
        // QueryWrapper<Like> wrapper = new QueryWrapper<>();
        // wrapper.eq("post_id", postId);
        // return likeMapper.selectCount(wrapper);
        
        return 0;
    }
} 